<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: options1Num == index }"
            @click="options1Fn(index, item.min, item.max)"
            v-for="(item, index) in options1"
            :key="index"
            >{{ item.txt }}</span
          >
          <!-- <span>我还可以兑换</span>
          <span>0-500个</span>
          <span>500-1000个</span>
          <span>1000-1500个</span>
          <span>1500-2500个</span> -->
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: item.isActive }"
            v-for="(item, index) in options2"
            :key="index"
            @click="options2Fn(index, item.type)"
          >
            {{ item.txt }}</span
          >
          <!-- <span>实物礼品</span>
          <span>虚拟礼品</span> -->
        </li>
      </ul>
<<<<<<< HEAD
      <List :arr="goodsListShow" />
      <div style="text-align: center">
        {{ isReachBottom ? "数据已经加载完了" : "正在加载中......" }}
        <div class=""></div>
      </div>
=======
      <List :arr="goodsList" />
>>>>>>> 42d7f4a3812cbaa13e12bafc11903e5f94f4bd8a
    </div>
  </div>
</template>
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { allGoodsAPI } from "@/request/api";
<<<<<<< HEAD
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils";
=======
>>>>>>> 42d7f4a3812cbaa13e12bafc11903e5f94f4bd8a
export default {
  data() {
    return {
      // 栏目  1->精品推荐 2-> 热门兑换 0->全部
      did: 0,
      // 1->实体商品 2->虚拟商品 0->全部
      type: 0,
      // 大于多少积分，最少是0
      min: 0,
      // 少于多少积分，最多是10000，当传入0的时候，会直接返回所有商品，无视后台逻辑。
      max: 0,
      // 搜索框关键字
      keyword: "",
      // 商品列表数组
      goodsList: [],
<<<<<<< HEAD
      // 显示商品数据
      goodsListShow: [],
      // 页数
      page: 1,
      // 数据条数
      size: 8,
      // 是否显示正在加载
      isLoading: false,
      // 是否滑动到底部
      isReachBottom: false,
=======
>>>>>>> 42d7f4a3812cbaa13e12bafc11903e5f94f4bd8a
      options1: [
        { min: 0, max: 10000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      // 选中的排序数组项目
      options1Num: 0,
      // 分类数组项
      options2: [
        { type: 0, txt: "全部", isActive: true },
        { type: 1, txt: "实体商品", isActive: false },
        { type: 2, txt: "虚拟商品", isActive: false },
      ],
    };
  },
  components: {
    Crumb,
    List,
  },
  watch: {
    // 监听keyword
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        this.keyword = newVal;
        // 重新渲染页面
        this.goodsSearch();
      },
    },
  },

  async created() {
    console.log("全部商品页面");
    // let res = await allGoodsAPI({
    //   did: this.did,
    //   type: this.type,
    //   min: this.min,
    //   max: this.max,
    //   keyword: this.keyword,
    // });
    // this.goodsList = res.data;
    // 新的参数
    this.keyword = this.$route.query.keyword || "";
    // 渲染页面
    this.goodsSearch();
  },
<<<<<<< HEAD
  mounted() {
    window.addEventListener("scroll", this.scrollFn);
    console.log("监听");
  },
  destroyed() {
    window.removeEventListener("scroll", this.scrollFn);
    console.log("取消监听");
  },
=======
>>>>>>> 42d7f4a3812cbaa13e12bafc11903e5f94f4bd8a
  methods: {
    // 异步发送请求
    async goodsSearch() {
      let res = await allGoodsAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      this.goodsList = res.data;
<<<<<<< HEAD
      this.goodsListShow = this.goodsList.filter((item, index) => index < 8);

      // 一进来就初始化
      this.page = 1;
      this.isReachBottom = false;
      // 没有数据默认滚动到底部
      if (this.goodsListShow.length >= this.goodsList.length) {
        this.isReachBottom = true;
      }
=======
>>>>>>> 42d7f4a3812cbaa13e12bafc11903e5f94f4bd8a
    },
    options1Fn(index, min, max) {
      this.options1Num = index;
      this.min = min;
      this.max = max;
      this.goodsSearch();
    },
    options2Fn(index, type) {
      this.options2.forEach((item) => {
        item.isActive = false;
      });
      this.options2[index].isActive = true;
      this.type = type;
      this.goodsSearch();
    },
<<<<<<< HEAD
    scrollFn() {
      // console.log(
      //   "滚动了",
      //   getScrollTop(),
      //   getClientHeight(),
      //   getScrollHeight()
      // );
      if (getScrollTop() + getClientHeight() >= getScrollHeight() - 50) {
        console.log("***即将加载数据");
        // 判断是否已经到底部
        if (this.goodsListShow.length == this.goodsList.length) {
          this.isReachBottom = true;
          return;
        }

        // 加载状态为false,可以加载
        if (this.isLoading == false) {
          this.isLoading = true;
          this.page++;
          // 话滑动过快就多次触发循环---解决方法(节流)
          // 节流设置,让一段代码,在同一个时间内不会重复执行
          setTimeout(() => {
            for (
              var i = this.size * (this.page - 1);
              i < this.size * this.page;
              i++
            ) {
              if (i == this.goodsList.length ) return;
              this.goodsList[i] && this.goodsListShow.push(this.goodsList[i]); //短路运算
              console.log(
                "要往show里面push的数据 ",
                this.page,
                this.goodsList[i]
              );
            }

            this.isLoading = false;
          }, 500);
        }
      }
    },
  },
};
</script>

=======
  },
};
</script>

>>>>>>> 42d7f4a3812cbaa13e12bafc11903e5f94f4bd8a
<style lang="less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
</style>
